package com.runchinaup.sifitbledome.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

import com.runchinaup.sifitbledome.R;
import com.runchinaup.sifitbledome.ble.DataInterface;
import com.runchinaup.sifitbledome.ble.conn.ConnResult;
import com.runchinaup.sifitbledome.domain.SendData;

/**
 * Created by liber on 2018/3/16.
 */

public class HeartRateActivity extends Activity implements DataInterface.DataCallback {

    private TextView centerTitle;
    private TextView heartRate;

    private DataInterface dataInterface = DataInterface.getDataInterface();
    private SendData sendData = new SendData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.ui_heart_rate);
        initView();
    }

    private void initView() {

        centerTitle = findViewById(R.id.centerText);
        heartRate = findViewById(R.id.heart_rate);

        centerTitle.setText("心率检测");
        dataInterface.resgistData(this);
        startTotestHeart();
    }

    //发送开始测试心率
    private void startTotestHeart() {
        sendData.setCommend((byte) 9);
        sendData.setKey((byte) 2);
        sendData.setLength(new byte[]{0x01, 0x00});
        sendData.setValue(new byte[]{0x01});
        dataInterface.sendToBle(sendData);

    }

    @Override
    protected void onPause() {
        super.onPause();
        sendData.setValue(new byte[]{0x00});
        dataInterface.sendToBle(sendData);
    }

    @Override
    public void onConnectResult(ConnResult connResult) {

    }

    @Override
    public void getEletricity(byte[] data) {

    }

    //5500430a08010043
    @Override
    public void getHeartRate(final byte[] data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (data[3] == 0x0a) {
                    if (data[4] == 0x08) {
                        if (data[7] > 0) {
                            heartRate.setText(String.format("%02d", (data[7] & 0xff)));
                        }
                    }
                }
            }
        });
    }
}
